package adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.sxpjyy.monkey.mobilenurse.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import objects.HE_XJMXXM;
import objects.HE_XJXM;
import objects.XJData;

/**
 * Created by Monkey on 2017/4/10.
 * 宣教项目适配器（内层）
 */

class XJXMListAdapter extends BaseExpandableListAdapter {

    private List<HE_XJXM> mXJXMList;
    private Context mContext;
    private LayoutInflater inflater;
    private List<Boolean> isParentChoiced;
    private List<Boolean> isAllSelectMode;
    private List<List<Boolean>> isChildChoiced;

    XJXMListAdapter(Context context) {
        mContext = context;
        inflater = LayoutInflater.from(mContext);
        isParentChoiced = new ArrayList<>();
        isChildChoiced = new ArrayList<>();
        isAllSelectMode = new ArrayList<>();
    }

    void setmXJXMList(List<HE_XJXM> mXJXMList) {
        this.mXJXMList = mXJXMList;
        Collections.sort(mXJXMList, new Comparator<HE_XJXM>() {
            public int compare(HE_XJXM arg0, HE_XJXM arg1) {
                return arg0.getXMBS().compareTo(arg1.getXMBS());
            }
        });
        if (isAllSelectMode.size() == 0) {
            for (int i = 0; i < mXJXMList.size(); i++) {
                isParentChoiced.add(false);
                isAllSelectMode.add(false);
                int size = mXJXMList.get(i).getList().size();
                List<Boolean> list = new ArrayList<>();
                for (int j = 0; j < size; j++) {
                    list.add(false);
                }
                isChildChoiced.add(list);
            }
        }
    }

    List<Boolean> getIsParentChoiced() {
        return isParentChoiced;
    }

    List<List<Boolean>> getIsChildChoiced() {
        return isChildChoiced;
    }

    void setIsParentChoiced(int position, boolean isChoiced) {
        isParentChoiced.set(position, isChoiced);
    }

    void setIsAllSelectMode(int position, boolean isAllSelect) {
        isAllSelectMode.set(position, isAllSelect);
    }

    void changeChildChoiced(int groupPos, int childPos, boolean isChoiced) {
        isChildChoiced.get(groupPos).set(childPos, isChoiced);
    }

    @Override
    public int getGroupCount() {
        if (mXJXMList != null && mXJXMList.size() > 0) {
            return mXJXMList.size();
        }
        return 0;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (mXJXMList != null && mXJXMList.get(groupPosition) != null && mXJXMList.get(groupPosition).getList() != null) {
            return mXJXMList.get(groupPosition).getList().size();
        }
        return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        if (mXJXMList != null && mXJXMList.get(groupPosition) != null) {
            return mXJXMList.get(groupPosition);
        }
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if (mXJXMList.get(groupPosition).getList() != null && mXJXMList.get(groupPosition).getList().get(childPosition) != null) {
            return mXJXMList.get(groupPosition).getList().get(childPosition);
        }
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ParentViewHolder holder;
        if (convertView == null) {
            holder = new ParentViewHolder();
            convertView = inflater.inflate(R.layout.item_xjxm, parent, false);
            holder.textView = (TextView) convertView.findViewById(R.id.xjxm_text);
            convertView.setTag(holder);
        } else {
            holder = (ParentViewHolder) convertView.getTag();
        }
        if (mXJXMList != null && mXJXMList.size() > 0) {
            HE_XJXM heXjxm = mXJXMList.get(groupPosition);
            if (heXjxm != null) {
                String xmmc = heXjxm.getXMMC() == null ? "" : heXjxm.getXMMC();
                holder.textView.setText(xmmc);
            }
        }
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final ChildViewHolder holder;
        List<HE_XJMXXM> list = mXJXMList.get(groupPosition).getList();
        Collections.sort(list, new Comparator<HE_XJMXXM>() {
            public int compare(HE_XJMXXM arg0, HE_XJMXXM arg1) {
                return arg0.getMXBS().compareTo(arg1.getMXBS());
            }
        });
        if (convertView == null) {
            holder = new ChildViewHolder();
            convertView = inflater.inflate(R.layout.item_xjxmmx, parent, false);
            holder.checkBox = (CheckBox) convertView.findViewById(R.id.xjxmmx_checkbox);
            holder.textView = (TextView) convertView.findViewById(R.id.xjxmmx_text);
            convertView.setTag(holder);
        } else {
            holder = (ChildViewHolder) convertView.getTag();
        }
        if (list.size() > 0) {
            HE_XJMXXM heXjmxxm = list.get(childPosition);
            if (isAllSelectMode.get(groupPosition)) {
                holder.checkBox.setChecked(isParentChoiced.get(groupPosition));
                isChildChoiced.get(groupPosition).set(childPosition, isParentChoiced.get(groupPosition));
            } else {
                holder.checkBox.setChecked(isChildChoiced.get(groupPosition).get(childPosition));
            }
            if (heXjmxxm != null) {
                String mxmc = heXjmxxm.getMXMC() == null ? "" : heXjmxxm.getMXMC();
                holder.textView.setText(mxmc);
            }
        }
        holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isAllSelectMode.set(groupPosition, false);
                isChildChoiced.get(groupPosition).set(childPosition, holder.checkBox.isChecked());
                doSetDataSet(groupPosition);
            }
        });
//        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//               doSetDataSet(groupPosition);
//            }
//        });
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private class ParentViewHolder {
        private TextView textView;
    }

    private class ChildViewHolder {
        private CheckBox checkBox;
        private TextView textView;
    }

    void doSetDataSet(int groupPos) {
        Map<String, List<String>> dataSet = XJData.getDataSets();
        if (dataSet == null) {
            dataSet = new HashMap<>();
        }
        String xmys = mXJXMList.get(groupPos).getXMYS();
        List<String> list = dataSet.get(xmys);
        if (list == null) {
            list = new ArrayList<>();
        }
        for (int i = 0; i < isChildChoiced.size(); i++) {
            for (int j = 0; j < isChildChoiced.get(i).size(); j++) {
                String mxbs = mXJXMList.get(i).getList().get(j).getMXBS();
                if (isChildChoiced.get(i).get(j)) {
                    if (!list.contains(mxbs)) {
                        list.add(mxbs);
                    }
                } else {
                    if (list.contains(mxbs)) {
                        list.remove(mxbs);
                    }
                }
            }
        }
        dataSet.remove(xmys);
        dataSet.put(xmys, list);
        XJData.setDataSets(dataSet);
    }
}
